"""
Apollo.io爬虫 - Scrapy版本
"""
import scrapy
import json
from typing import List, Dict, Any, Optional
from .base_spider import BaseScrapySpider, SpiderFactory

class ApolloSpider(BaseScrapySpider):
	name = 'apollo'

	def __init__(self, name=None, search_queries: Optional[List[str]] = None, status_callback=None, api_key: Optional[str] = None, **kwargs):
		super().__init__(name or self.name, query=None, status_callback=status_callback, **kwargs)
		self.search_queries = search_queries or []
		self.api_key = api_key
		self.base_url = "https://api.apollo.io/v1"
		self.custom_settings = {
			'DOWNLOAD_DELAY': 1,
			'CONCURRENT_REQUESTS': 1,
			'CONCURRENT_REQUESTS_PER_DOMAIN': 1,
		}

	def _get_start_requests(self):
		if not self.search_queries or not self.api_key:
			return []
		headers = {'Content-Type': 'application/json', 'Cache-Control': 'no-cache'}
		for q in self.search_queries:
			body = json.dumps({
				'api_key': self.api_key,
				'q_organization_name': q,
				'page': 1,
				'per_page': 10
			})
			yield scrapy.Request(
				url=f"{self.base_url}/organizations/search",
				method='POST',
				headers=headers,
				body=body,
				callback=self.parse_companies,
				meta={'search_query': q}
			)

	def parse_companies(self, response):
		try:
			data = json.loads(response.text)
			companies = data.get('organizations', [])
			headers = {'Content-Type': 'application/json', 'Cache-Control': 'no-cache'}
			for company in companies:
				company_id = company.get('id')
				if not company_id:
					continue
				body = json.dumps({
					'api_key': self.api_key,
					'organization_id': company_id,
					'page': 1,
					'per_page': 50
				})
				yield scrapy.Request(
					url=f"{self.base_url}/people/search",
					method='POST',
					headers=headers,
					body=body,
					callback=self.parse_contacts,
					meta={'company': company, 'search_query': response.meta.get('search_query', '')}
				)
		except json.JSONDecodeError:
			self.logger.error("解析Apollo公司搜索响应失败")

	def parse_contacts(self, response):
		try:
			data = json.loads(response.text)
			people = data.get('people', [])
			company = response.meta.get('company', {})
			for person in people:
				yield {
					"search_query": response.meta.get('search_query', ''),
					"company_name": company.get('name', ''),
					"contact_name": f"{person.get('first_name', '')} {person.get('last_name', '')}".strip(),
					"email": person.get('email', ''),
					"phone": person.get('phone', ''),
					"title": person.get('title', ''),
					"linkedin_url": person.get('linkedin_url', ''),
					"source": "apollo",
					"confidence": 0.95,
					"raw_data": {"company": company, "person": person}
				}
		except json.JSONDecodeError:
			self.logger.error("解析Apollo联系人响应失败")

	def is_free_source(self) -> bool:
		return False

	def get_rate_limit(self) -> float:
		return 1.0

	def get_cost_info(self) -> Dict[str, Any]:
		return {
			"is_free": False,
			"rate_limit": self.get_rate_limit(),
			"description": "Apollo.io API - 付费服务，按API调用次数计费",
			"api_key_required": True,
			"credits_per_request": 1
		}

SpiderFactory.register("apollo", ApolloSpider)